package server;

import java.io.*;
import java.net.ServerSocket;
import java.net.Socket;
import java.util.Date;

/**
 * 创建服务器并启动
 * 请求
 * 响应
 *
 */
public class Server {
    private ServerSocket server;

    public static void main(String[] args) {
        Server server=new Server();
        server.start();
    }

    /**
     * 启动服务器
     */
    public void start(){
        try {
            server=new ServerSocket(8866);
            this.receive();
        } catch (IOException e) {
            e.printStackTrace();
        }
    }

    /**
     * 接收客户端
     */
    private void receive(){
        try {
            Socket client=server.accept();
            //请求
            Request req=new Request(client.getInputStream());
            //响应
            Response rep=new Response(client.getOutputStream());
            rep.println("<html><head><title>server服务器</title>");
            rep.print("</head><body>");
            rep.println("欢迎:").println(req.getParameter("userName")).println("回来");
            rep.println("</body></html>");
            rep.pushToClient(200);
        } catch (IOException e) {
            //e.printStackTrace();
        }
    }

    /**
     * 停止服务器
     */
    public void stop(){

    }
}
